import router from "./index";
import store from "../store/index";

router.beforeEach((to, from, next) => {
  if (!store.state.Token) {
    if (
      to.matched.length > 0 &&
      !to.matched.some(record => record.meta.requiresAuth)
    ) {
      next();
    } else {
      next({
        path: "/login"
      });
    }
  } else {
    if (!store.state.permission.permissionList) {
      store.dispatch("permission/FETCH_PERMISSION").then(() => {
        next({
          path: to.path
        });
        // next();
      });
    } else {
      if (to.path !== "/login") {
        next();
      } else {
        next(from.fullPath);
      }
    }
  }
});
